<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>流程任务管理</title>
    <#include "/pubHeadStyle.html">
</head>
<body>
<title>流程历史管理</title>
<div style="position: relative;height: 100%;">
    <div class="tablesize roles" style="position: absolute;top:0;right:0px;bottom: 0;left: 0px;padding: 10px">
        <table id="history" lay-filter="history"></table>
    </div>
</div>

<script type="text/html" id="headerToolsBar">
    <div class="ccpui-enterSel">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-sm" lay-event="query">
            <i class="layui-icon layui-icon-search"></i>
            <span>查询</span>
        </a>
        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">
            <i class="layui-icon layui-icon-delete"></i>
            <span>删除</span>
        </a>
        <a class="layui-btn layui-btn-sm" lay-event="reset">
            <i class="layui-icon layui-icon-refresh"></i>
            <span>重置</span>
        </a>
    </div>
    <form class="layui-form" action="" lay-filter="instanceHistoryQueryForm" style="margin-top:10px">
        <div class="table-top-filter">
            <div class="table-filter-alone">
                <label class="layui-form-label">流程定义名称</label>
                <input type="text" name="Q_processName_SL" id="processName" class="layui-input filter-value"
                       placeholder="流程定义名称"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">流程实例标题</label>
                <input type="text" name="Q_subject_SL" id="subject" class="layui-input filter-value"
                       placeholder="流程实例标题"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">创建人</label>
                <input type="text" name="Q_creator_SL" id="creator" class="layui-input filter-value" placeholder="创建人"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">创建时间</label>
                <input type="text" name="Q_createTime_DL" id="createTime" class="layui-input filter-value"
                       placeholder="开始时间"/>
                <span style="float:left;margin-left:46px">至</span>
                <input type="text" name="Q_endCreateTime_DG" id="endCreateTime" class="layui-input filter-value"
                       placeholder="结束时间"
                       style="margin-left:60px"/>
            </div>
        </div>
    </form>
    </div>
</script>

<script type="text/html" id="historyBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs " lay-event="get">明细</a>
    <a class="layui-btn layui-btn-xs" lay-event="log">操作日志</a>
    <a class="layui-btn layui-btn-xs " lay-event="copyTo" >抄送转发</a>
</script>

<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','table', 'layer', 'laydate', 'util', 'commons', 'setter', 'totalFilter', 'common','admin'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var $ = layui.$;
        var setter = layui.setter;
        var laydate = layui.laydate;
        var commons = layui.commons;
        var common = layui.common;
        var totalFilter = layui.totalFilter;
        var admin = layui.admin;
        var tableH = commons.tabHeight(tableH);
        var dataPrivacyLevel;
        var privacyLevel = ${privacyLevel};

        loadTable(serverPath.workflowPath + setter.workflowLinkPath + 'processRun/history');

        //第一个实例
        function loadTable(url) {
            table.render({
                elem: '#history',
                id: 'history'
                , cellMinWidth: 80
                , title: '流程历史管理'
                , height: tableH
                ,defaultToolbar: ['filter', 'print']
                , url: url
                , method: 'post'
                , toolbar: '#headerToolsBar'
                ,autoSort: false
                , page: true
                , cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'globalFlowNo', title: '工单号', sort: true, minWidth: 150,align: 'center'}
                    , {field: 'processName', title: '流程定义名称', sort: true, align: 'center'}
                    , {field: 'subject', title: '流程实例标题', sort: true, minWidth: 330,align: 'center'}
                    , {field: 'creator', title: '创建人', sort: true, align: 'center'}
                    , {
                        field: 'createTime', title: '创建时间',minWidth: 200, sort: true, align: 'center', templet: function (d) {
                            if (d.createTime== undefined) {
                                return "";
                            } else {
                                return util.toDateString(d.createTime.time, "yyyy-MM-dd HH:mm:ss");
                            }
                        }
                    }, {
                        field: 'endTime', title: '结束时间',minWidth: 200, sort: true, align: 'center', templet: function (d) {
                            if (d.endTime == undefined) {
                                return "";
                            } else {
                                return util.toDateString(d.endTime.time, "yyyy-MM-dd HH:mm:ss");
                            }
                        }
                    }, {
                        field: 'duration', title: '持续时间', sort: true, align: 'center', templet: function (d) {
                            if (d.duration === undefined||d.duration===0) {
                                return "";
                            }else {
                                //计算出相差天数
                                var days = Math.floor(d.duration / (24 * 3600 * 1000));
                                //计算出小时数
                                var leave1 = d.duration % (24 * 3600 * 1000);   //计算天数后剩余的毫秒数
                                var hours = Math.floor(leave1 / (3600 * 1000));
                                //计算相差分钟数
                                var leave2 = leave1 % (3600 * 1000);        //计算小时数后剩余的毫秒数
                                var minutes = Math.floor(leave2 / (60 * 1000));

                                return days + "天" + hours + "时" + minutes + "分";
                            }
                            }
                    }
                    , {
                        field: 'status', title: '状态', sort: true, align: 'center', templet: function (d) {
                            if (d.status == undefined) {
                                return "";
                            }
                            if (d.status == 1) {
                                return '<span style="color: #15cc08;">正在运行</span>';
                            }
                            if (d.status == 2) {
                                return '<span style="color: #cc0c00;">结束</span>';
                            }
                            if (d.status == 3) {
                                return '<span style="color: rgba(123,73,0,0.94);">人工结束</span>';
                            }
                            if (d.status == 5) {
                                return '<span style="color: #15cc08;">撤销</span>';
                            }
                            if (d.status == 6) {
                                return '<span style="color: #15cc08;">驳回</span>';
                            }
                            if (d.status == 7) {
                                return '<span style="color: #15cc08;">追回</span>';
                            }
                        }
                    }
                    , {fixed: 'right', title: '管理', toolbar: '#historyBar', width: 150, align: 'center'}
                ]]
                ,done:function () {
                    commons.reflashDate({startName: '#createTime', endName: '#endCreateTime'});
                    common.tableSelectCon('.ccpui-enterSel .layui-form input','a[lay-event=query]','.ccpui-enterSel')
                }
            });
            totalFilter.init('history');
        }
        // 监听行工具事件
        table.on('toolbar(history)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data; //获取选中行数据
            switch (obj.event) {
                case 'query':
                    common.tableConditionRender("history", "instanceHistoryQueryForm", {where: {}}, false);
                    break;
                case 'reset':
                    common.tableConditionRender("history", "instanceHistoryQueryForm", {where: {}}, true);
                    break;
                case 'del':
                    if (data.length === 0) {
                        layer.msg('请先选择一行数据！');
                    } else {
                        var selectedIdArray = [];
                        for (var i = 0; i < data.length; i++) {
                            selectedIdArray.push(data[i].runId);
                        }
                        var selectedIds = selectedIdArray.join(",");
                        layer.confirm('确定删除吗？', function () {
                            $.ajax({
                                url: serverPath.workflowPath + setter.workflowLinkPath + "processRun/del?runId=" + selectedIds,
                                type: "DELETE",
                                dataType: 'json',
                                success: function (data) {
                                    if(data.code == 0){
                                        //成功后的提示
                                        layer.alert(data.msg);
                                        //执行重载
                                        table.reload('history', {
                                            page: {curr: 1, limit: 10}
                                        });
                                        totalFilter.init('history');
                                    }else {
                                        layer.alert("删除失败");
                                    }

                                },
                                error: function (XMLHttpRequest) {
                                    layer.alert("请求失败！" + XMLHttpRequest.status);
                                }
                            })
                        })
                    }
                    break;
            }
        });
        table.on('tool(history)', function (obj) {
                var data = obj.data;
                var runId = data.runId;
                var procInstId = data.procInstId;
                // 获取密级
                $.ajax({
                    url: serverPath.workflowPath + setter.workflowLinkPath+"processRun/getPrivacyLevelByRunId",
                    data:{runId: runId},
                    type: "post",
                    dataType: 'json',
                    success: function (result) {
                        dataPrivacyLevel = result;
                        if (privacyLevel < dataPrivacyLevel) {
                            layer.msg('密级不足,无法执行该操作', {icon: 5});
                        }else{
                            switch (obj.event) {
                                case 'del':
                                    layer.confirm("确认删除吗？", {title: '删除确认'}, function () {
                                        $.ajax({
                                            url: serverPath.workflowPath + setter.workflowLinkPath + "processRun/del?runId=" + runId,
                                            type: 'DELETE',
                                            dataType: 'json',
                                            success: function (data) {
                                                //成功后的提示
                                                layer.alert(data.msg);
                                                //执行重载
                                                table.reload('history', {
                                                    page: {curr: 1, limit: 10}
                                                });
                                                totalFilter.init('history');
                                            },
                                            error: function () {
                                                layer.alert("删除失败");
                                            }
                                        });
                                    });
                                    break;
                                case 'log':
                                    parent.layer.open({
                                        type: 2,
                                        title: "操作日志",
                                        shade: 0.3,
                                        left: 200,
                                        area: ['80%', '80%'],
                                        content: serverPath.workflowPath +  setter.workflowLinkPath +  'processRun/view/form?runId=' + runId
                                    });
                                    break;
                                case 'get':
                                    window.open(serverPath.workflowPath +  setter.workflowLinkPath + 'processRun/view/get?runId=' + runId);
                                    break;
                                case 'copyTo':
                                    parent.layer.open({
                                        type: 2,
                                        title: "抄送转发",
                                        shade: 0.3,
                                        left: 200,
                                        area: ['80%', '80%'],
                                        content: '${ctx}/workflow/processCopyTo/view/list?runId=' + runId
                                    });
                                    break;

                            }
                        }
                    }
                });
            }
        );


        commons.reflashDate({startName: '#createTime', endName: '#endCreateTime'});
        window.Initialization = function () {
            commons.reflashDate({startName: '#createTime', endName: '#endCreateTime'});
        };
        common.tableSort("history",{show:window.Initialization});
    });
</script>
</body>
</html>


